package Mr.Right.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import Mr.Right.util.model.DateTime;

public class CalendarTool {
	/**
	 * 以天为单位获得日期列表，最近last天的日期
	 * @param last
	 * @return
	 */
	public static List<DateTime> getLastDays(Integer last){
		
		List<DateTime> lastDay = new ArrayList<DateTime>();
		for(int i = 0 ;i < last ; i++){
			DateTime dateTime = getDateTime(i+1);
			lastDay.add(dateTime);
		}
		return lastDay;
	}
	/**
	 * 以周为单位获得日期列表，最近last个周的日期
	 * @param last
	 * @return
	 */
	public static List<DateTime> getLastWeeks(Integer last){
		List<DateTime> lastWeek = new ArrayList<DateTime>();
		for(int i = 0 ;i < last ; i++){
			DateTime dateTime = getDateTime(7*i+1);
			lastWeek.add(dateTime);
		}
		return lastWeek;
	}
	/**
	 * 以月为单位获得日期列表，最近last个月的日期
	 * @param last
	 * @return
	 */
	public static List<DateTime> getLastMonths(Integer last){
		List<DateTime> lastMonths = new ArrayList<DateTime>();
		Calendar cal = new GregorianCalendar();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		DateTime dateTime;
		cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR)-1);
		for(int i =1;i<=last;i++){
			String result=format.format(cal.getTime());
			dateTime = new DateTime();
            dateTime.setDate(result);
            dateTime.setWeekOfyear(cal.get(Calendar.WEEK_OF_YEAR));
            dateTime.setMonth(cal.get(Calendar.MONTH)+1);
            dateTime.setYear(cal.get(Calendar.YEAR));
			cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1);
			
			lastMonths.add(dateTime);
			
		}
		return lastMonths;
	}
	/**
	 * 获取指定日期的前N天的日期，如果day为空,则从当前日期开始算起
	 * @param day yyyyMMdd
	 * @param n 必须dayu0
	 * @return yyyy/MM/dd
	 */
	public static DateTime getBeforeDay_N(String day,Integer n){
		if(n<=0){
			throw new RuntimeException("参数不合法，N="+n+" 参数N必须大于0");
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date date;
		DateTime dateTime = new DateTime();
		try {
			if(null == day || "".equals(day)){
				date = new Date();
			}else{
				date = format.parse(day);
			}
			Calendar cal = Calendar.getInstance();
			cal.setFirstDayOfWeek(Calendar.MONDAY);
			cal.setTime(date);
			cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR)-n);
			
            String result=format.format(cal.getTime());
            dateTime.setDate(result);
            dateTime.setWeekOfyear(cal.get(Calendar.WEEK_OF_YEAR));
            dateTime.setMonth(cal.get(Calendar.MONTH)+1);
            dateTime.setYear(cal.get(Calendar.YEAR));
			return dateTime;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateTime;
	}
	/**
	 * ��ȡָ�����ڵĺ�N�����ڵ�����
	 * @param day yyyyMMdd N
	 * @return yyyy/MM/dd
	 */
	public static DateTime getAfterDay_N(String day,Integer n){
		if(n<=0){
			throw new RuntimeException("参数不合法，N="+n+" 参数N必须大于0");
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date date;
		DateTime dateTime = new DateTime();
		try {
			if(null == day || "".equals(day)){
				date = new Date();
			}else{
				date = format.parse(day);
			}
			Calendar cal = Calendar.getInstance();
			cal.setFirstDayOfWeek(Calendar.MONDAY);
			cal.setTime(date);
			cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR)+n);
			
            String result=format.format(cal.getTime());
            dateTime.setDate(result);
            dateTime.setWeekOfyear(cal.get(Calendar.WEEK_OF_YEAR));
            dateTime.setMonth(cal.get(Calendar.MONTH)+1);
            dateTime.setYear(cal.get(Calendar.YEAR));
			return dateTime;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateTime;
	}
	/**
	 * ��ȡָ�����ڵ�ǰN������ڣ����dayΪ��,��ӵ�ǰ���ڿ�ʼ����
	 * @param day yyyyMMdd
	 * @param n �������0
	 * @return yyyy/MM/dd
	 */
	public static DateTime getBeforeMonth_N(String day,Integer n){
		if(n<=0){
			throw new RuntimeException("参数不合法，N="+n+" 参数N必须大于0");
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date date;
		DateTime dateTime = new DateTime();
		try {
			if(null == day || "".equals(day)){
				date = new Date();
			}else{
				date = format.parse(day);
			}
			Calendar cal = Calendar.getInstance();
			cal.setFirstDayOfWeek(Calendar.MONDAY);
			cal.setTime(date);
			cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-n);
			
            String result=format.format(cal.getTime());
            dateTime.setDate(result);
            dateTime.setWeekOfyear(cal.get(Calendar.WEEK_OF_YEAR));
            dateTime.setMonth(cal.get(Calendar.MONTH)+1);
            dateTime.setYear(cal.get(Calendar.YEAR));
			return dateTime;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateTime;
	}
	/**
	 * 获取指定日期的后N天所在的日期
	 * @param day yyyyMMdd N
	 * @return yyyy/MM/dd
	 */
	public static DateTime getAfterMonth_N(String day,Integer n){
		if(n<=0){
			throw new RuntimeException("参数不合法，N="+n+" 参数N必须大于0");
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date date;
		DateTime dateTime = new DateTime();
		try {
			if(null == day || "".equals(day)){
				date = new Date();
			}else{
				date = format.parse(day);
			}
			Calendar cal = Calendar.getInstance();
			cal.setFirstDayOfWeek(Calendar.MONDAY);
			cal.setTime(date);
			cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+n);
			
            String result=format.format(cal.getTime());
            dateTime.setDate(result);
            dateTime.setWeekOfyear(cal.get(Calendar.WEEK_OF_YEAR));
            dateTime.setMonth(cal.get(Calendar.MONTH)+1);
            dateTime.setYear(cal.get(Calendar.YEAR));
			return dateTime;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateTime;
	}
	/**
	 * 转换字符串所代表的日期的格式
	 * @param date 需要转换的字符串日期
	 * @param formatOfDate 需要转换的字符串日期的原格式
	 * @param format_dis 目标格式
	 * @return
	 */
	public static String convertDateFormat(String str_date,String formatOfstr_date,String format_dis){
		SimpleDateFormat sr = new SimpleDateFormat(formatOfstr_date);
		SimpleDateFormat dis = new SimpleDateFormat(format_dis);
		try {
			Date date = sr.parse(str_date);
			return dis.format(date);
			
		} catch (ParseException e) {
			
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取系统的当前时间
	 * @return yyyy/MM/dd hh:mm:ss
	 */
	public static String getCurrDayTime(){
		return new SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(new Date());
	}
	/**
     * 获取当前时日期的前N天的日期
     * @param n ǰn��
     * @return DateTime
     */
   public static DateTime getDateTime(int n){
       DateTime dateTime=new DateTime();
       Calendar cal=Calendar.getInstance();
       cal.add(Calendar.DATE,-n);
       SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); 
       String result=sdf.format(cal.getTime());
       dateTime.setDate(result);
       dateTime.setWeekOfyear(cal.get(Calendar.WEEK_OF_YEAR));
       dateTime.setMonth(cal.get(Calendar.MONTH)+1);
       dateTime.setYear(cal.get(Calendar.YEAR));
       return dateTime;
   }
	public static void main(String[] args) throws ParseException {
		
		System.out.println(getLastMonths(12));
	}
}
